package com.bosssoft.hr.train.spring.dao;

import com.bosssoft.hr.train.spring.demo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wang
 * @Time: 2022/3/6
 */


public class UserDaoImpl implements UserDao{

     @Autowired
    private JdbcTemplate jdbcTemplate;


    public Boolean add(User user) {
        String sql="insert into user (id,name) values(?,?)";
        int i=jdbcTemplate.update(sql,user.getId(),user.getName());
        if (i<=0){
            return false;
        }else
        return true;
    }

    public Boolean deleteById(int id) {
         int i=   jdbcTemplate.update("delete  from user where id=?",id);
         if (i<=0){
             return false;
         }else
        return true;
    }

    public Boolean deleteById(ArrayList ids) {
        int[] i=jdbcTemplate.batchUpdate("delete from user where id=?",ids);
        if (i.length<=0){
            return false;
        }else
            return true;
    }

    public List<User> selectByName(String name) {
        List<User> users=jdbcTemplate.query("select * from user where name = ?",new BeanPropertyRowMapper<User>(User.class),name);
        return users.isEmpty()?null:users;
    }

    public User selectById(int id) {
        List<User> users=jdbcTemplate.query("select * from user where id = ?",new BeanPropertyRowMapper<User>(User.class),id);
        return users.isEmpty()?null:users.get(0);
    }




}
